<script type="text/javascript" src="<?php echo base_url(); ?>static/select2/select2.min.js" ></script>
<script type="text/javascript" src="<?php echo base_url(); ?>static/js/selectSearchGlobal.js"></script>
<!--
<link type="text/css" href="<?php echo base_url(); ?>ajaxupload/ajaxfileupload.css" rel="stylesheet" /> 
<script src="<?php echo base_url(); ?>ajaxupload/jquery.js" type="text/javascript"></script>
<script src="<?php echo base_url(); ?>ajaxupload/ajaxfileupload.js" type="text/javascript"></script>
-->

<script>
//SCRIPT INI HARUS DIATAS, TIDAK BOLEH DIBAWAH
$("#r_name").change(function()
{
    //alert(this.id.substr(2));    
	var nik = $(this).val();
	var DataString = {id:nik};
	$.ajax({
			type: "POST",
			url : "<?php echo site_url('search_global/globals_nik_detail')?>",
			dataType: 'json',
			data: DataString,
			success: function(data)
			{
				$("#r_id").val(data.DetailEmp.HR_ID);
                $("#r_nama").val(data.DetailEmp.NAME);
                $("#r_mgrid").val(data.DetailEmp.MANAGER_ID);
				$("#r_mgr").val(data.DetailEmp.MANAGER);
				$("#r_dept").val(data.DetailEmp.ORG);
				$("#r_job").val(data.DetailEmp.JOB_POSITION);														
			}
		});
});
/*
$("#D_Regional").change(function()
{
    //alert(this.id.substr(2));
    $("#pDmRegional").val(data.Show_Region.REGIONAL_ID);
    $("#pDmRegionalName").val(data.Show_Region.REGIONAL_NAME);	    
	
    var nik = $(this).val();
	var DataString = {id:nik};
	$.ajax({
			//type: "POST",
			//url : "<?php echo site_url('search_global/globals_nik_detail')?>",
			dataType: 'json',
			data: DataString,
			success: function(data)
			{
				$("#pDmRegional").val(data.Show_Region.REGIONAL_ID);
                $("#pDmRegionalName").val(data.Show_Region.REGIONAL_NAME);														
			}
		});
        
});
*/
</script>
<script>
    function cancel() {
        $.post("<?= site_url() ?>search_global", function(data) {
            window.location = '<?php echo base_url(); ?>search_global/search_global';
        });
    }
    function editPubRomMailSave() {
        var desc=$('#deskripsi').val();
            var nilai3=desc.trim(); 
        $.post("<?= site_url() ?>search_global/edit_reqSave", {
            pDmTujuan 		:  $('#D_Area').val(),
        	pDmType 		:  $('#cargo_type').val(),
        	pDmDesc 		:  nilai3,
        	pDmBerat		:  $('#weight').val(),
        	pAtasanNik		:  $('#r_mgrid').val(),
            pAtasanName		:  $('#r_mgr').val(),
        	pDmRegional	    :  $('#pDmRegional').val(),
        	pDmLokasi		:  $('#lokasi_jemput').val(),
        	pDmFloor		:  $('#lantai').val(),
        	pDmCategory	    :  $('#category').val(),
        	pDmTime		    :  $('#waktu_jemput').val(),
        	pJasaKurir		:  $('#courier').val(),
        	pDmrId			:  $('#pCtrlID').val(),
            pCType			:  $('#pCType').val()
        },
        function(data) {
            window.location = '<?php echo base_url(); ?>search_global/search_global';
        }
        );
    }
</script>
<script>
    
    function setRegional(){
        var regname=$("#D_Regional").val();
        //var regid=$("#pDmRegional").val(data.REGIONAL_ID.HR_ID);
        
        //var obj = JSON.parse(document.getElementById('D_Regional').value);
        //$("#pDmRegional").val(data.Show_Region.REGIONAL_ID);
        //$("#pDmRegionalName").val(data.Show_Region.REGIONAL_NAME);
        //var obj = JSON.parse('Show_Region');

        //document.getElementById("pDmRegional").innerHTML = obj.Show_Region[0].AREA_ID;
        document.getElementById("pDmRegional").value=regname;
    }
    
</script>

<?php 
	foreach($getDetail as $row){
		$vDmrId		    =	$row['DMR_ID'];
    	$vDmrReqId	    =	$row['DMR_UNTUK_NIK'];
    	$vDmrType	    =	$row['DMR_COURIER_TYPE'];
    	$vDmrArea	    =	$row['DMR_TUJUAN'];
    	$vDmrRegion    	=	$row['DMR_REGIONAL'];
    	$vDmrLoc		=	$row['DMR_PENJEMPUTAN'];
    	$vDmrFloor		=	$row['DMR_FLOOR'];
    	$vDmrJenis		=	$row['DMR_TYPE'];
    	$vDmrCateg		=	$row['DMR_CATEGORY'];
    	$vDmrTime		=	$row['DMR_WAKTU_PENJEMPUTAN'];
    	$vDmrAtasanId	=	$row['DMR_ATASAN_APP_ID'];
    	$vDmrDesc		=	$row['DMR_DESCRIPTION'];
    	$vID		    =	$row['ID'];
    	$vName		    =	$row['NAME'];
    	$vOrg	     	=	$row['ORG'];
    	$vJob		    =	$row['JOB'];
    	$vAtasan	    =	$row['ATASAN'];
 	}
    /*
	if(empty($org)){
		   $org = dbora_getorg("$nik");
	}*/
?>

<div id="top" >
<form id="formarea" role="form" method="post" action="" class="form-horizontal"  enctype="multipart/form-data"> 
    <input type="hidden" value="<?php echo $cRequestNo; ?>" id="pCtrlID" name="pCtrlID" />
    <input type="hidden" value="<?php echo $cType; ?>" id="pCType" name="pCType"/>
    <input type="hidden" value="<?php echo $sesNik;?>" id="pNik" name="pNik"/>
    
    
    <div class="col-md-11">
			<div class="panel panel-default">
				<div class="panel-heading">
					<h4 class="panel-title"><b class="caret"></b><b> Request By</b></h4>
				</div>
				<div class="panel-body">
					<div class="col-xs-12 col-sm-6 col-md-6">
						<div class="form-group">
							<label class="control-label col-md-3">Name(*useless)<span style="color:red;">*</span></label>
							<div class="col-md-7">
                            <input type="hidden" value="<?php echo $vName;?>" id="r_nama" class="select2 form-control input-sm" readonly=""/>
                            <input type="text" value="" id="r_name" class="select2 form-control input-sm" placeholder="<?php echo $vName;?>"/>
                            </div>
						</div>
						<div class="form-group">
							<label class="control-label col-md-3">ID Number(*useless)</label>
							<div class="col-md-4">
							<input type="text" value="<?php echo $vID;?>" id="r_id" class="form-control input-sm" readonly=""/> 
							<!-- Edit by Surya Alam Gultom ~ before id="r_nik" -->
							</div> 
						</div>
						<div class="form-group">
							<label class="control-label col-md-3">Manager</label>
							<div class="col-md-7">
                            <input type="hidden" value="<?php echo $vDmrAtasanId;?>" id="r_mgrid" class="form-control input-sm" readonly=""/>
							<input type="text" value="<?php echo $vAtasan;?>" id="r_mgr" class="form-control input-sm" readonly=""/>
							<!-- Edit by Surya Alam Gultom ~ before id="r_manager" -->
							</div>
						</div>
					</div>
					<div class="col-xs-6 col-sm-6 col-md-6">
						<div class="form-group">
							<label class="control-label col-md-4">Departement</label>
							<div class="col-md-7">
							<input type="text" value="<?php echo $vOrg;?>" id="r_dept" class="form-control input-sm" readonly=""/>
							<!-- Edit by Surya Alam Gultom ~ before id="r_department" -->
							</div>
						</div>
						<div class="form-group">
							<label class="control-label col-md-4">Job Position</label>
							<div class="col-md-8">
							<input type="text" value="<?php echo $vJob;?>" id="r_job" class="form-control input-sm" readonly=""/>
							</div>
						</div>
					</div>
				</div>
			</div>
            
            
			<div class="panel panel-default">
				<div class="panel-heading"><h4 class="panel-title"><b class="caret"></b><b> Form</b></h4></div>
				<div class="panel-body">
					<div class="col-sm-6 col-md-6">
						<div class="form-group">
							<label class="col-md-4">Courier<span style="color:red;">*</span></label> 
							<div class="col-md-5">
								<select id="courier" class="form-control input-sm" onchange="javascript:getArea()"> <!--pJasaKurir-->
									<option value="">Select Courier</option>
									<option value="DOM" <?php if($vDmrType=="DOM"){?>selected=""<?php } ?>>Domestic</option>
									<option value="INT" <?php if($vDmrType=="INT"){?>selected=""<?php } ?>>International</option>
									<option value="MES" <?php if($vDmrType=="MES"){?>selected=""<?php } ?>>Messenger</option>
								</select>
							</div>
						</div>
						<div class="form-group">
							<label class="col-md-4">Category</label>
							<div class="col-md-4">
								<select class="form-control input-sm" id="category"> <!--pDmCategory-->
									<option value="Reguler" <?php if($vDmrCateg=="Reguler"){?>selected=""<?php }?>>Reguler</option>
									<option value="Insidential" <?php if($vDmrCateg=="Insidential"){?>selected=""<?php }?>>Insidential</option>
								</select>
							</div>
						</div>					
						<div class="form-group">
							<label class="col-md-4">Cargo Type</label>
							<div class="col-md-4">
								<select class="form-control input-sm" id="cargo_type"> <!--pDmType-->
									<option value="Dokumen" <?php if($vDmrJenis=="Dokumen"){?>selected=""<?php }?>>Document</option>
									<option value="Non Dokumen" <?php if($vDmrJenis=="Non Dokumen"){?>selected=""<?php }?>>Non Document</option>
								</select>
							</div>
						</div>
						<div class="form-group">
							<label class="col-md-4">Location Pickup</label>
							<div class="col-md-4">
								<select class="form-control input-sm" id="lokasi_jemput"> <!--pDmLokasi-->
									<option value="User" <?php if($vDmrLoc=="User"){?>selected=""<?php }?>>User</option>
									<option value="Dmr" <?php if($vDmrLoc=="Dmr"){?>selected=""<?php }?>>Mailing Room</option>
								</select>
							</div>		
						</div>
						<div class="form-group">
							<label class="col-md-4">Time to Pickup</label>
							<div class="col-md-4">
								<select id="waktu_jemput" class="form-control input-sm"> <!--pDmTime-->
                                    <?php
                                    if($vDmrTime=="10"){
                                        ?>
                                        <option value="10" selected="">10:00</option>
                                        <?php       
                                    }
                                    else{
                                        ?>
                                        <option value="15" selected="">15:00</option>
                                        <?php
                                    }
                                    ?>
								</select>
							</div>
						</div>
					</div>
                    
					<div class="col-sm-6 col-md-6">
						<div class="form-group" id="Area">
							<label class="col-md-5">Destination Area<span style="color:red;">*</span></label>
							<div class="col-md-6">
								<select id="D_Area" class="form-control input-sm" onchange="javascript:getReg(this)"> <!--pDmTujuan-->
									<option readonly="" value="">Select Courier First</option> <!--$vDmrArea--> 
                                    <?php
                                    foreach($get_Area as $area){
                                        ?>
                                        <option value="<?php echo $area['AREA_NAME'];?>" <?php if($vDmrArea==$area['AREA_NAME']){?>selected=""<?php } ?>>
                                        <?php echo $area['AREA_NAME'];?>
                                        </option>
                                        <?php
                                    }
                                    ?>
								</select>
							</div>
						</div>
						<div class="form-group" id="Regional">
							<label class="col-md-4">Regional<span style="color:red;">*</span></label> <!--pDmRegional-->
							<div class="col-md-7"> <!--$vDmrRegion-->
                                <input type="hidden" value="<?php echo $vDmrRegion;?>" id="pDmRegional" class="form-control input-sm" readonly=""/>
								<input onchange="setRegional()" type="text" value="" id="D_Regional" class="form-control input-sm" placeholder="<?php echo $vDmrRegion;?>"/> 
                                
							</div>
						</div>
						<div class="form-group">
							<label class="col-md-4">Floor<span style="color:red;">*</span></label>
							<div class="col-md-2">
								<input type="text" value="<?php echo $vDmrFloor;?>" id="lantai" class="form-control input-sm" /> <!--pDmFloor-->
							</div>
							
							<label class="col-md-2">Weight(*useless)<span style="color:red;">*</span></label>
							<div class="col-md-2">
								<input type="text" value="" id="weight" class="form-control input-sm" />
							</div>
						</div>
						<div class="form-group">
							<label class="col-md-4">Description<span style="color:red;">*</span></label>
							<div class="col-md-8"><!--pDmDesc-->
								<textarea id="deskripsi" class="form-control input-sm" style="min-width:190px;max-width:200px;min-height:100px;max-height:200px;"><?php echo $vDmrDesc;?>
                                </textarea>
							</div>
						</div>
					</div>
                    
					<div class="row"> 
                        <div class="col-sm-12">
                            <div class="form-group">
                                <label class="control-label col-md-3"></label>
        						<div class="col-sm-2">
                                    <button type="button" class="btn btn-primary btn-block" onclick="editPubRomMailSave();">Submit</button>
                                </div>
                                <div class="col-sm-2">
                                    <button type="button" class="btn btn-default btn-block" onclick="cancel();" >Cancel</button>
                                </div>
    				        </div>
                        </div>                   
                    </div>
				</div>
			</div>
		</div>

</form>
</div>


<script type="text/javascript">
    $.validator.setDefaults({
        submitHandler: function() {
            $.post("<?= site_url() ?>public_room/insert_dmr", {
                r_id: $('#r_id').val(), 
                r_mgr: $('#r_mgr').val(), 
                courier: $('#courier').val(), 
				category: $('#category').val(), 
				cargo_type: $('#cargo_type').val(), 
				location: $('#lokasi_jemput').val(), 
				timed: $('waktu_jemput').val(), 
				D_Area: $('D_Area').val(), 
				D_Regional: $('D_Regional').val(), 
				lantai: $('#lantai').val(), 
				weight: $('#weight').val(), 
				deskripsi: $('#deskripsi').val() 
            },
            function(data) {
                $('#loader').html(data);
            });
        }
    });

    $(document).ready(function() {
        $("#formuser").keypress(function(e) {
            if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
                $.validator.setDefaults({
                    submitHandler: function() {
                        $.post("<?= site_url() ?>public_room/insert_dmr", {
                            r_id: $('#r_id').val(), 
							r_mgr: $('#r_mgr').val(), 
                			courier: $('#courier').val(), 
							category: $('#category').val(), 
							cargo_type: $('#cargo_type').val(), 
							location: $('#lokasi_jemput').val(), 
							timed: $('waktu_jemput').val(), 
							D_Area: $('D_Area').val(), 
							D_Regional: $('D_Regional').val(), 
							lantai: $('#lantai').val(), 
							weight: $('#weight').val(), 
							deskripsi: $('#deskripsi').val() 
                        },
                        function(data) {
                            $('#loader').html(data);
                        });
                    }
                });
            }
        });

        $("#formuser").validate();
    });

	function getArea()
	{
		var courier_id = {courier_id:$("#courier").val()};
		$.ajax({
				type: "POST",
				url : "<?php echo site_url('public_room/ShowArea');?>",
				data: courier_id,
				success: function(msg){$('#Area').html(msg);}
			});
	};
	
	function getReg(area_name)
	{
			var getarea = {area_name:area_name.value};
			$.ajax({
				type: "POST",
				url : "<?php echo site_url('public_room/ShowRegional');?>",
				data: getarea,
				dataType:'json',
				success: function(data)
				{
					$('#D_Regional').select2({
						id: function(bond){ 
						  //return bond.AREA_ID; //value yang ada di select
                          return bond.REGIONAL_NAME;
                        },
					ajax:({	
						type:"POST",
						dataType:'json',
						url: "<?php echo site_url('public_room/getRegional')?>",
						data: function(term)
						{
							return {
										SearchReg:data,
										term:term
							};
						},
						results: function(bond, page) {return {results: bond.Show_Region, more: (bond.Show_Region && bond.Show_Region.length == 10 ? true: false)}}
						}),
						formatResult: function(bond) {return '<div class="select2-user-result">' + bond.REGIONAL_NAME + '</div>'},
						formatSelection: function(bond){return bond.REGIONAL_NAME},
						initSelection: function(elem, cb){return elem}
					});
				}
			});
	};

</script>